<template>
    <div class="c-main" ref="tableH">
        <el-table :data="tableData" style="width: 100%" :max-height="maxH">
            <el-table-column fixed prop="date" label="日期" width="150" align="center" />
            <el-table-column prop="name" label="姓名" width="120" align="center" />
            <el-table-column prop="state" label="国家" width="120" align="center" />
            <el-table-column prop="city" label="城市" width="120" align="center" />
            <el-table-column prop="address" label="地区" width="600" align="center" />
            <el-table-column prop="age" label="年龄" width="120" align="center" />
            <el-table-column fixed="right" label="操作" width="160" align="center">
                <template #default>
                    <el-button type="danger" size="small" icon="Delete" @click="handleClick">
                        删除
                    </el-button>
                    <el-button type="primary" size="small" icon="Edit">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="pagina">
            <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                :page-sizes="[100, 200, 300, 400]"
                :small="small"
                :background="background"
                layout="total, sizes, prev, pager, next, jumper"
                :total="400"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange" />
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const handleClick = () => {
    console.log('点击了删除！');
    ElMessageBox.confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(() => {
            ElMessage({
                type: 'success',
                message: '操作成功',
            });
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '操作取消',
            });
        });
};

const tableH = ref();
let maxH = ref<string>();
onMounted(() => {
    let hdHeight = tableH.value?.clientHeight;
    maxH.value = hdHeight - 60 + 'px';
});

const currentPage = ref(1);
const pageSize = ref(100);
const small = ref(false);
const background = ref(false);
const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
};

const tableData = [
    {
        date: '2016-05-03',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        age: '12',
        tag: 'Home',
    },
    {
        date: '2016-05-02',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        age: '14',
        tag: 'Office',
    },
    {
        date: '2016-05-04',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        age: '15',
        tag: 'Home',
    },
    {
        date: '2016-05-01',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        age: '12',
        tag: 'Office',
    },
    {
        date: '2016-05-01',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        age: '20',
        tag: 'Office',
    },
    {
        date: '2016-05-01',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        age: '18',
        tag: 'Office',
    },
    {
        date: '2016-05-01',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        age: '36',
        tag: 'Office',
    },
];
</script>

<style lang="scss" scoped></style>
